package com.jetgege.smartbox.Guide;

import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.jetgege.smartbox.Base.BaseActivity;
import com.jetgege.smartbox.R;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class UserGuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {


    @BindView(R.id.vp_GuidePage)
    ViewPager vpGuidePage;
    @BindView(R.id.ll_GuidePoint)
    LinearLayout llGuidePoint;//放置圆点

    private int[] imageIdArray;//图片资源的数组
    private List<View> viewList;//图片资源的集合

    //实例化原点View
    private ImageView iv_point;
    private ImageView[] ivPointArray;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_use_guide;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //加载ViewPager
        initViewPager();

        //加载底部圆点
        initPoint();

    }

    /**
     * 加载底部圆点
     */
    private void initPoint() {

        //根据ViewPager的item数量实例化数组
        ivPointArray = new ImageView[viewList.size()];
        //循环新建底部圆点ImageView，将生成的ImageView保存到数组中
        int size = viewList.size();
        for (int i = 0; i < size; i++) {
            iv_point = new ImageView(this);
           // iv_point.setLayoutParams(new ViewGroup.LayoutParams(20, 20));
            // iv_point.setPadding(0, 0, 30, 0);//left,top,right,bottom
            //拉开小圆点的距离
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(0, 0, 20, 0);
            lp.width=25;
            lp.height = 25;
            iv_point.setLayoutParams(lp);
            ivPointArray[i] = iv_point;
            //第一个页面需要设置为选中状态，这里采用两张不同的图片
            if (i == 0) {
                iv_point.setBackgroundResource(R.drawable.point_pressed);
            } else {
                iv_point.setBackgroundResource(R.drawable.point_normal);
            }
            //将数组中的ImageView加入到ViewGroup
            llGuidePoint.addView(ivPointArray[i]);
        }


    }

    /**
     * 加载图片ViewPager
     */
    private void initViewPager(){

        //实例化图片资源
        imageIdArray = new int[]{R.drawable.guide1, R.drawable.guide2, R.drawable.guide3};
        viewList = new ArrayList<>();
        //获取一个Layout参数，设置为全屏
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);

        //循环创建View并加入到集合中
        int len = imageIdArray.length;
        for (int i = 0; i < len; i++) {
            //new ImageView并设置全屏和图片资源
            ImageView imageView = new ImageView(this);
            imageView.setLayoutParams(params);
            imageView.setBackgroundResource(imageIdArray[i]);
            //将ImageView加入到集合中
            viewList.add(imageView);
        }
        //View集合初始化好后，设置Adapter
        vpGuidePage.setAdapter(new GuidePageAdapter(viewList));
        //设置滑动监听
        vpGuidePage.setOnPageChangeListener(this);
    }


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    /**
     * 滑动后的监听
     *
     * @param position
     */
    @Override
    public void onPageSelected(int position) {
        //循环设置当前页的标记图
        int length = imageIdArray.length;
        for (int i = 0; i < length; i++) {
            ivPointArray[position].setBackgroundResource(R.drawable.point_pressed);
            if (position != i) {
                ivPointArray[i].setBackgroundResource(R.drawable.point_normal);
            }

        }
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @OnClick(R.id.ib_GuideBack)
    public void onViewClicked() {
        finish();
        overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
    }

}
